
layui.use(['upload', 'form', 'laydate'], function () { //此段代码必不可少
    var form = layui.form;
    var upload = layui.upload;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#entranceTime'
    })
    //执行实例
    var uploadInst = upload.render({
        elem: '#icon_change', //绑定元素

        url: LOCALHOSTURL+'/upload', //上传接口

        accept: 'images',
        acceptMime: 'image/*',
        xhrFields: {
            withCredentials: true //默认情况下，标准的跨域请求是不会发送cookie的
        },
        headers: {
            "Authorization": $.cookie("token")
        },
        crossDomain: true,
        before: function(obj){
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#icon').attr('src', result); //图片链接（base64）
            });
        },
        // done: function (res) {
        //上传完毕回调
        //     return layer.msg(res.message);
        // ("#icon").attr("src", res.iconName);
        // },
        error: function () {
        // 请求异常回调
        // 演示失败状态，并实现重传
        // var demoText = $('#demoText');
        // demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
        // demoText.find('.demo-reload').on('click', function(){
        //     uploadInst.upload();
        // });
        // return layer.alert('上传失败');
        }
    });

    $(function () {
        getCode();
        getRoleList();
        form.render();
    })
    form.render();
    //获取码表数据
    function getCode() {
        let data = null;
        if (sessionStorage.getItem("code")!=null) {
            data = JSON.parse(sessionStorage.getItem("code"));
        }else {
            let result = ajaxModel("/user/getCodeList", "get");
            data = result.data;
            sessionStorage.setItem("code", JSON.stringify(data));
        }
        console.log(data);
        $("#sex").html("<option value=''>全部</option>");
        $.each(data.sex,function (i,sex) {
            $("#sex").append("<option value='"+ sex.code +"'>"+ sex.name +"</option>");
        })
        $("#graduate").html("<option value=''>全部</option>");
        $.each(data.graduate,function (i,graduate) {
            $("#graduate").append("<option value='"+ graduate.code +"'>"+ graduate.name +"</option>");
        })
    }

    function getRoleList() {
        let data = null;
        if (sessionStorage.getItem("role")!=null) {
            data = JSON.parse(sessionStorage.getItem("role"));
        }else {
            let result = ajaxModel("/role/getRoleList", "get");
            data = result.data;
            sessionStorage.setItem("role", JSON.stringify(data));
        }
        console.log(data);
        $("#role").html("<option value=''>全部</option>");
        $.each(data,function (i,role) {
            $("#role").append("<option value='"+ role.id +"'>"+ role.name +"</option>");
        });
    }
});
